<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title></title>
  <link rel="stylesheet" type="text/css" href="css/litewebchat.min.css" />
  <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
  <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
  <link rel="stylesheet" type="text/css" href="static/css/mui.min.css">
    <link rel="stylesheet" type="text/css" href="static/css/mui.imageviewer.css">
    <link rel="stylesheet" type="text/css" href="static/css/chat-im.css">
</head>
<style>
html,body{
  height: 100%;
  margin: 0;
  padding: 0;
  width: 100%;
}
</style>
<body onload="openSocket()">



    <!-- 主容器 -->
    <div class="lite-chatbox"> 

    <!-- cleft 左 -->
    
    <footer>
       
        <div class="footer-center">
            <textarea id="msg_text" type="text" class="input-text"></textarea>
           
        </div>
        <div class="footer-right">
            
            <div class="mui-btn mui-btn-success " id="sendMessage" onclick="sendMessage()">发送</div>
        </div>
    </footer>
        
</body>
<script>
    var socket;
    var username = JSON.parse(sessionStorage.getItem("username"))
    var userid = JSON.parse(sessionStorage.getItem("userid"))
    function openSocket() {
        if(typeof(WebSocket) == "undefined") {
            console.log("您的浏览器不支持WebSocket");
        }else{
            console.log("您的浏览器支持WebSocket");
            //实现化WebSocket对象，指定要连接的服务器地址与端口  建立连接
            //等同于socket = new WebSocket("ws://localhost:8888/xxxx/im/25");
            //var socketUrl="${request.contextPath}/im/"+$("#userId").val();
            var socketUrl="https://prehealth.top/pre2.0/imserver/"+30195632147;
            socketUrl=socketUrl.replace("https","ws").replace("http","ws");
            console.log(socketUrl);
            if(socket!=null){
                socket.close();
                socket=null;
            }
            socket = new WebSocket(socketUrl);
            //打开事件
            socket.onopen = function() {
                console.log("websocket已打开");
                //socket.send("这是来自客户端的消息" + location.href + new Date());
            };
            //获得消息事件
            socket.onmessage = function(msg) {
                console.log(msg.data);
                //发现消息进入    开始处理前端触发逻辑
                $('.lite-chatbox').append("<div class='cleft cmsg'><img class='headIcon radius' ondragstart='return false;'oncontextmenu='return false;'src='./img/A.jpg'/><span class='name'>同学</span><span class='content'>"+msg.data+"</span> </div>");
            };
            //关闭事件
            socket.onclose = function() {
                console.log("websocket已关闭");
            };
            //发生了错误事件
            socket.onerror = function() {
                console.log("websocket发生了错误");
            }
        }
    }
    
    function sendMessage() {

        console.log('{"toUserId":"'+10+'","contentText":"'+$(".input-text").val()+'"}');

        
        socket.send('{"toUserId":"'+10+'","contentText":"'+$(".input-text").val()+'"}');
        
        $('.lite-chatbox').append("<div class='cright cmsg'><img class='headIcon radius' ondragstart='return false;'oncontextmenu='return false;'src='./img/B.jpg'/><span class='name'>医生</span><span class='content'>"+$(".input-text").val()+"</span> </div>");
        
    }
</script>
</html>